package com.shangqing.system.service.impl;

import com.shangqing.system.domain.caoyuan.vo.CityVo;
import com.shangqing.system.domain.snow.Snow;
import com.shangqing.system.mapper.CityMapper;
import com.shangqing.system.mapper.SnowEarlyMapper;
import com.shangqing.system.mapper.SnowGradeMapper;
import com.shangqing.system.mapper.SnowMapper;
import com.shangqing.system.service.SnowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SnowServiceImpl implements SnowService {
    @Autowired
    private SnowMapper snowMapper;
    @Autowired
    private SnowGradeMapper snowGradeMapper;
    @Autowired
    private SnowEarlyMapper snowEarlyMapper;
    @Autowired
    private CityMapper cityMapper;
    @Override
    public List<Snow> selectAll() {
        List<Snow> snowList = new ArrayList<>();
        List<Snow> list = snowMapper.selectall();
        for (Snow snow : list
        ) {
            snow.setSnowGrade(snowGradeMapper.selectById(snow.getGradeId()));
            snow.setSnowEarly(snowEarlyMapper.selectById(snow.getEarlyId()));
            CityVo city = cityMapper.select(snow.getCityId());//获取当前地址
            city.setCities(cityVos(city));// 递归获取地址
            snow.setCityVo(city);
            snowList.add(snow);

        }
        return snowList;
    }
    private CityVo cityVos(CityVo list) {
        if (list.getParentId() == 0) {
            return null;      //递归出口
        } else {
            CityVo vo = cityMapper.select(list.getParentId());//获取父ID对应的地址
            CityVo cityVo = new CityVo();//封装
            cityVo.setId(vo.getId());
            cityVo.setCityName(vo.getCityName());
            cityVo.setParentId(vo.getParentId());
            cityVo.setCities(cityVos(vo)); //进行递归进口
            return cityVo;
        }
    }
}
